﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Entities;
using C.Unite;
using ChanExt;
using MySoft.Data;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;

namespace ArtLand.OA.Project.ADM
{
    public partial class Adm_BASICDList : CommonPageBase
    {
        #region Inti
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Grid1.AllowCellEditing = false;
                // Drp_YERNUB.Items.Add(new ListItem((DateTime.Now.Year - 1).ToString() + "年", (DateTime.Now.Year - 1).ToString()));
                // Drp_YERNUB.Items.Add(new ListItem((DateTime.Now.Year).ToString() + "年", (DateTime.Now.Year).ToString()));
                // Drp_YERNUB.SelectedValue = DateTime.Now.Year.ToString();

                Grid1.Title = Grid1.Title + " - " + Request["dname"];

                DataBaseHelper.BindDrp(Drp_MaterUnit, EnumData.DataBaseEunm.Unit);
                DataBaseHelper.BindDrp(Drp_MaterBrand, EnumData.DataBaseEunm.MaterBrand);
                DataBaseHelper.BindDrp(Drp_MaterColor, EnumData.DataBaseEunm.MaterColor);
                DataBaseHelper.BindDrp(Drp_MaterModel, EnumData.DataBaseEunm.MaterModel);


                JObject defaultObj = new JObject();
                defaultObj.Add("MaterNo", "");
                defaultObj.Add("MaterTitle", "");
                defaultObj.Add("MaterUnit", "");
                defaultObj.Add("MaterColor", "");
                defaultObj.Add("MaterBrand", "");
                defaultObj.Add("MaterModel", "");
                defaultObj.Add("REMARK", "");
                btnNew.OnClientClick = Grid1.GetAddNewRecordReference(defaultObj, false);

                BindGrid(GetWhere());
            }
        }
        #endregion

        #region BindGrid
        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = TbsDbSession.Default.From<Adm_BASICD>().Where(wc).Count();

            List<Adm_BASICD> items = TbsDbSession.Default.From<Adm_BASICD>().Where(wc).OrderBy(Adm_BASICD._.ID.Desc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();

            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }



        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }


        #endregion

        #region Events

        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            if (!string.IsNullOrEmpty(Request["did"]))
            {
                wc = wc && Adm_BASICD._.BATYPE == int.Parse(Request["did"]);
            }
            //if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            //{
            //    wc = wc && (Adm_BUDGET._.YERNUB.Contain(ttbxMyBox2.Text) || V_EmpInfo._.EMPLID.Contain(ttbxMyBox2.Text));
            //}
            return wc;
        }



        #region 搜索框
        //// 点击 TwinTriggerBox 的搜索按钮
        //protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        //{
        //    if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
        //    {
        //        // 执行搜索动作
        //        BindGrid(GetWhere());
        //        ttbxMyBox2.ShowTrigger1 = true;
        //    }
        //    else
        //    {
        //        Alert.ShowInTop("请输入你要搜索的关键词！");
        //    }
        //}

        //// 点击 TwinTriggerBox 的取消按钮
        //protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        //{
        //    // 执行清空动作

        //    ttbxMyBox2.Text = "";
        //    ttbxMyBox2.ShowTrigger1 = false;
        //    BindGrid(GetWhere());
        //} 
        #endregion

        protected void Grid1_RowCommand(object sender, ChanExt.GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                TbsDbSession.Default.Delete<Adm_BASICD>(Adm_BASICD._.ID == id);
                Alert.ShowInTop("删除成功！");
                BindGrid(GetWhere());
            }
        }

        protected void Grid1_RowDataBound(object sender, ChanExt.GridRowEventArgs e)
         {
        //    int id = int.Parse(e.Values[10].ToString());
        //    string tipmessage = e.Values[4].ToString();
        //    string mail = e.Values[6].ToString();
        //    string data = "";
        //    if (e.Values[5].ToString() != "")
        //        data = Convert.ToDateTime(e.Values[5].ToString()).ToString("yyyy-MM-dd");
        //    string datanow = DateTime.Now.ToString("yyyy-MM-dd");
        //    if (data.Trim() == datanow.Trim())
        //    {
        //        bool bflag = Regex.IsMatch(mail, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        //        if (bflag)
        //        {
        //            Adm_MailSendLogs c = TbsDbSession.Default.From<Adm_MailSendLogs>().Where(Adm_MailSendLogs._.BASID == id).ToFirst();
        //            if (c == null)
        //            {
        //                Adm_MailSendLogs maillog = new Adm_MailSendLogs();
        //                maillog.DeAttach();
        //                maillog.BASID = id;
        //                maillog.Mail = mail;
        //                maillog.SendTime = DateTime.Now;
        //                TbsDbSession.Default.Insert<Adm_MailSendLogs>(maillog);
        //                ToolHelper.sendMail(mail, tipmessage, tipmessage);
        //            }
        //            else
        //            {
        //                e.Values[7] = "<img src=\"/icon/accept.png\" /><span ext:qtip=\"已于" + c.SendTime.Value.ToString() + "发送\">已发送</span>";//"<span ext:qtip=\"asdf\" ><img src=\"/icon/accept.png\" /></span>";
        //            }
        //        }
        //        //else
        //        //    e.Values[6] += "<img src=\"/icon/error.png\" />";
        //        highlightRows.Text += e.RowIndex.ToString() + ",";
        //    }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Dictionary<int, Dictionary<string, string>> modifiedDict = Grid1.GetModifiedDict();
            foreach (int rowIndex in modifiedDict.Keys)
            {
                int rowID = Convert.ToInt32(Grid1.DataKeys[rowIndex][0]);
                UpdateDataRow(modifiedDict[rowIndex], rowID);
            }
            returnvalue += modifiedDict.Keys.Count();
            // 新增数据
            List<Dictionary<string, string>> newAddedList = Grid1.GetNewAddedList();
            for (int i = newAddedList.Count - 1; i >= 0; i--)
            {
                UpdateDataRow(newAddedList[i], 0);
            }
            returnvalue += newAddedList.Count;
            if (returnvalue > 0)
            {
                BindGrid(GetWhere());
                Alert.Show("保存成功！");
            }
            else
                Alert.Show("您未对任何行的数据进行修改！");
        }


        private static void UpdateDataRow(Dictionary<string, string> rowDict, int Dtype)
        {
            Adm_BASICD bud = new Adm_BASICD();
            if (Dtype == 0)
            {
                bud.DeAttach();
            }
            else
            {
                bud.ID = Dtype;
                bud.Attach();
            }
            if (rowDict.ContainsKey("MaterNo"))
            {
                bud.MaterNo = rowDict["MaterNo"];
            }
            if (rowDict.ContainsKey("MaterTitle"))
            {
                bud.MaterTitle = rowDict["MaterTitle"];
            }
            if (rowDict.ContainsKey("MaterUnit"))
            {
                bud.MaterUnit = rowDict["MaterUnit"];
            }

            if (rowDict.ContainsKey("MaterColor"))
            {
                bud.MaterColor = rowDict["MaterColor"];
            }
            if (rowDict.ContainsKey("MaterBrand"))
            {
                bud.MaterBrand = rowDict["MaterBrand"];
            }
            if (rowDict.ContainsKey("MaterModel"))
            {
                bud.MaterModel = rowDict["MaterModel"];
            }
            //if (rowDict.ContainsKey("TipTitle"))
            //{
            //    bud.MaterColor = rowDict["TipTitle"];
            //}
            //if (rowDict.ContainsKey("TipData"))
            //{
            //    bud.TipData = Convert.ToDateTime(rowDict["TipData"]);
            //}

            //if (rowDict.ContainsKey("TipMail"))
            //{
            //    bud.TipMail = rowDict["TipMail"];
            //}

                bud.INVQTY = 0;

            if (rowDict.ContainsKey("REMARK"))
            {
                bud.REMARK = rowDict["REMARK"];
            }
            bud.BATYPE = int.Parse(HttpContext.Current.Request["did"]);
            bud.USERID = CookInfo.EAdminid();
            if (Dtype == 0)
            {
                TbsDbSession.Default.Insert<Adm_BASICD>(bud);
            }
            else
            {
                TbsDbSession.Default.Update<Adm_BASICD>(bud);
            }
        }
        #endregion
    }
}